package 简单题目;

import java.util.Arrays;

public class 数组mex {

	public static void main(String[] args) {
		int[] a = { 4, 5, 6, 7 };
		System.out.println(findArrayMex(a, a.length));
	}

	public static int findArrayMex(int[] A, int n) {
		Arrays.sort(A);
		int tar = 0;
		int pos = 1;
		for (int i = 0; i < n; i++) {
			// 小于0跳过
			if (A[i] <= 0)
				continue;
			// 相等去掉
			while (i != n - 1 && A[i] == A[i + 1])
				i++;

			// 判断跳出循环
			if (A[i] != pos) {
				tar = pos;
				break;
			}

			pos++;
		}
		return tar == 0 ? A[n - 1] + 1 : tar;
	}

}
